package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LeaveRetire;
import com.ruoyi.system.service.ILeaveRetireService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 离退休申请Controller
 * 
 * @author ruoyi
 * @date 2023-11-21
 */
@RestController
@RequestMapping("/system/leaveRetire")
public class LeaveRetireController extends BaseController
{
    @Autowired
    private ILeaveRetireService leaveRetireService;


    /**
     * 获取辞职或退休的人的平均工作时间
     * @return
     */
    @Anonymous
    @GetMapping("/getAverage")
    public AjaxResult getLeaveRetireAverageTime(){
        List<LeaveRetire> leaveRetires = leaveRetireService.selectLeaveRetireList(null);
        //根据type等于0，1分为两个list
        List<LeaveRetire> leaveList = new ArrayList<>();
        List<LeaveRetire> retireList = new ArrayList<>();
        for(LeaveRetire lr : leaveRetires){
            if(lr.getType().equals("0")){
                leaveList.add(lr);
            }else if(lr.getType().equals("1")){
                retireList.add(lr);
            }
        }

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,Double> map = new HashMap<>();
        map.put("退休",0.0);
        map.put("辞职",0.0);

        //首先处理辞职的
        double totalTime = 0.0;
        for(LeaveRetire lr : leaveList){
            String workingTime = lr.getWorkingTime();
            //workingTime格式 10年6月，获取总月数
            int years = Integer.parseInt(workingTime.split("年")[0]);
            int months = Integer.parseInt(workingTime.split("年")[1].split("月")[0]);
            double totalMonths = years * 12 + months;
            totalTime += totalMonths;
        }
        map.put("辞职",map.get("辞职")+(double)totalTime/leaveList.size());


        totalTime = 0.0;
        for(LeaveRetire lr : retireList){
            String workingTime = lr.getWorkingTime();
            //workingTime格式 10年6月，获取总月数
            int years = Integer.parseInt(workingTime.split("年")[0]);
            int months = Integer.parseInt(workingTime.split("年")[1].split("月")[0]);
            double totalMonths = years * 12 + months;
            totalTime += totalMonths;
        }
        map.put("退休",map.get("退休")+(double)totalTime/retireList.size());
//        list.add(map);

        for (Map.Entry<String, Double> entry : map.entrySet()) {
            Map<String,Object> item = new HashMap<>();
            item.put("value", entry.getValue());
            item.put("name", entry.getKey());
            list.add(item);
        }
        return AjaxResult.success(list);
    }

    @Anonymous
    @GetMapping("/getPeopleNum")
    public AjaxResult getLeaveRetirePeopleNum(){
        List<LeaveRetire> leaveRetires = leaveRetireService.selectLeaveRetireList(null);
        List<Map<String, Object>> list = new ArrayList<>();

        Map<String, Integer> map = new HashMap<>();
        map.put("辞职", 0);
        map.put("退休", 0);

        for(LeaveRetire lr : leaveRetires){
            if(lr.getType().equals("0")){
                map.put("辞职", map.get("辞职") + 1);
            }
            if(lr.getType().equals("1")){
                map.put("退休",map.get("退休") + 1);
            }
        }


        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            Map<String, Object> item = new HashMap<>();
            item.put("value", entry.getValue());
            item.put("name", entry.getKey());
            list.add(item);
        }

        return AjaxResult.success(list);
    }

    /**
     * 查询离退休申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:leaveRetire:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveRetire leaveRetire)
    {
        startPage();
        List<LeaveRetire> list = leaveRetireService.selectLeaveRetireList(leaveRetire);
        return getDataTable(list);
    }

    /**
     * 导出离退休申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:leaveRetire:export')")
    @Log(title = "离退休申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveRetire leaveRetire)
    {
        List<LeaveRetire> list = leaveRetireService.selectLeaveRetireList(leaveRetire);
        ExcelUtil<LeaveRetire> util = new ExcelUtil<LeaveRetire>(LeaveRetire.class);
        util.exportExcel(response, list, "离退休申请数据");
    }

    /**
     * 获取离退休申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:leaveRetire:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveRetireService.selectLeaveRetireById(id));
    }

    /**
     * 新增离退休申请
     */
    @PreAuthorize("@ss.hasPermi('system:leaveRetire:add')")
    @Log(title = "离退休申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveRetire leaveRetire)
    {
        return toAjax(leaveRetireService.insertLeaveRetire(leaveRetire));
    }

    /**
     * 修改离退休申请
     */
    @PreAuthorize("@ss.hasPermi('system:leaveRetire:edit')")
    @Log(title = "离退休申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveRetire leaveRetire)
    {
        return toAjax(leaveRetireService.updateLeaveRetire(leaveRetire));
    }

    /**
     * 删除离退休申请
     */
    @PreAuthorize("@ss.hasPermi('system:leaveRetire:remove')")
    @Log(title = "离退休申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveRetireService.deleteLeaveRetireByIds(ids));
    }
}
